﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DTS
{
    class AlgorithmRSI : Algorithm
    {
        int Period;
        public AlgorithmRSI(int days)
        {
            Period = days;
            this.name = "RSI" + days.ToString();
        }
        public AlgorithmRSI()
        {
            Period = 15;
            this.name = "RSI15";
        }
        public override int getDecision(HistoriaSpolki hs, DateTime Today)
        {
            Double rsi = getRSI(hs, Today);
            Double rsip = getRSI(hs, Today.AddDays(-1));
            if (rsi == -1 || rsip == -1) return 0;
            if (rsi > 30 && rsip < 30) return 1;
            if (rsi < 70 && rsip > 70) return -1;
            return base.getDecision(hs, Today);
        }
        public override int getDecision(HistoriaSpolki hs, double cena, DateTime Today)
        {
            return getDecision(hs, Today);
        }

        public Double getRSI(HistoriaSpolki hs, DateTime today)
        {
            var hsPrep = hs.getPrevOfData(today, Period);
            List<Double> Up = new List<double>();
            List<Double> Down = new List<double>();
            var dane = hsPrep.getDane();
            if (dane.Count == 0) return -1;
            dane.Reverse();
            for (int i = 1; i < dane.Count-1; i++)
            {
                if (dane[i - 1].GetClose() < dane[i].GetClose())
                {
                    Up.Add(dane[i].GetClose() - dane[i - 1].GetClose());
                    Down.Add(0);
                }
                else
                {
                    Up.Add(0);
                    Down.Add(dane[i - 1].GetClose() - dane[i].GetClose());
                }
            }
            Up.Reverse();
            Down.Reverse();
            Double RS = Pomocnik.EMA(Up) / Pomocnik.EMA(Down);
            return 100.0f - ( 100.0f / (1.0f + RS) );
        }
    }
}
